/**
 * @(#)SeachDataController.java 2017年6月2日
 * Copyright(C) 2017年6月2日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.domian.MarkStatRecordBean;
import org.jsbd.boss.service.IMarkStatRecordService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.SortList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 新查询数据
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/markStartRecord")
public class MarkStatRecordController {
	public static final Logger LOGGER = LoggerFactory.getLogger(MarkStatRecordController.class);
	
	@Autowired
	private IMarkStatRecordService markStatRecordService;
	
	@RequestMapping(value = "newTaskStatisticsModel")
	public String showNewTaskModel(Model model) throws Exception {
		return "/markStartRecord/newTaskStatisticsList";
	}
	
	@RequestMapping(value = "retentTaskStatisticsModel")
	public String showRetentTaskModel(Model model) throws Exception {
		return "/markStartRecord/retentTaskStatisticsList";
	}
	
	@RequestMapping(value = "retent1TaskStatisticsModel")
	public String showRetent1TaskModel(Model model) throws Exception {
		return "/markStartRecord/retent1TaskStatisticsList";
	}
	
	@ResponseBody
	@RequestMapping("/getNewTaskPageList")
	public Map<String,Object> getNewTaskPageList(DataGridModel page,MarkStatRecordBean markStatRecordBean) throws Exception {
		if (StringUtils.isNotBlank(markStatRecordBean.getStart())) {
			markStatRecordBean.setStart(markStatRecordBean.getStart().replaceAll("-", ""));	
		}
		if (StringUtils.isNotBlank(markStatRecordBean.getEnd())) {
			markStatRecordBean.setEnd(markStatRecordBean.getEnd().replaceAll("-", ""));		
		}
		if(StringUtils.isBlank(markStatRecordBean.getStart())
				&&StringUtils.isBlank(markStatRecordBean.getEnd())){
			markStatRecordBean.setDay(DateUtil.getCurrentDateStr());
		}
		String appName = markStatRecordBean.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			markStatRecordBean.setKeywords(keywords);
		}
		markStatRecordBean.setPageInfo(page);
		return markStatRecordService.getNewTaskPageList(markStatRecordBean);
	}
	
	@ResponseBody
	@RequestMapping("/getRetentTaskPageList")
	public Map<String,Object> getRetentTaskPageList(DataGridModel page,MarkStatRecordBean markStatRecordBean) throws Exception {
		if (StringUtils.isNotBlank(markStatRecordBean.getStart())) {
			markStatRecordBean.setStart(markStatRecordBean.getStart().replaceAll("-", ""));	
		}
		if (StringUtils.isNotBlank(markStatRecordBean.getEnd())) {
			markStatRecordBean.setEnd(markStatRecordBean.getEnd().replaceAll("-", ""));		
		}
		if(StringUtils.isBlank(markStatRecordBean.getStart())
				&&StringUtils.isBlank(markStatRecordBean.getEnd())){
			markStatRecordBean.setDay(DateUtil.getCurrentDateStr());
		}
		String appName = markStatRecordBean.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			markStatRecordBean.setKeywords(keywords);
		}
		Map<String, Object> map = markStatRecordService.getRetentTaskPageList(page, markStatRecordBean);
		List<MarkStatRecordBean> list = (List<MarkStatRecordBean>) map.get("rows");
				
		if ("executeRate".equals(markStatRecordBean.getSort())) {
			SortList<MarkStatRecordBean> sortList = new SortList<MarkStatRecordBean>();  
			sortList.Sort(list, "getExecuteRate", markStatRecordBean.getOrder());  
		}
		return map;
	}
	
	/**
	 * 留存一次查询
	 * @param page
	 * @param markStatRecordBean
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/get1RetentTaskPageList")
	public Map<String,Object> get1RetentTaskPageList(DataGridModel page,MarkStatRecordBean markStatRecordBean) throws Exception {
		if (StringUtils.isNotBlank(markStatRecordBean.getStart())) {
			markStatRecordBean.setStart(markStatRecordBean.getStart().replaceAll("-", ""));	
		}
		if (StringUtils.isNotBlank(markStatRecordBean.getEnd())) {
			markStatRecordBean.setEnd(markStatRecordBean.getEnd().replaceAll("-", ""));		
		}
		if(StringUtils.isBlank(markStatRecordBean.getStart())
				&&StringUtils.isBlank(markStatRecordBean.getEnd())){
			markStatRecordBean.setDay(DateUtil.getCurrentDateStr());
		}
		String appName = markStatRecordBean.getAppName();
		if (StringUtils.isNotBlank(appName)) {
			String[] appNames = appName.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : appNames) {
				keywords.add(keyword);
			}
			markStatRecordBean.setKeywords(keywords);
		}
		Map<String, Object> map = markStatRecordService.get1RetentTaskPageList(page, markStatRecordBean);
		List<MarkStatRecordBean> list = (List<MarkStatRecordBean>) map.get("rows");
				
		if ("executeRate".equals(markStatRecordBean.getSort())) {
			SortList<MarkStatRecordBean> sortList = new SortList<MarkStatRecordBean>();  
			sortList.Sort(list, "getExecuteRate", markStatRecordBean.getOrder());  
		}
		return map;
	}
}
